package cj.primariaiquest.integration.endpoints;

import org.apache.log4j.Logger;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.channel.interceptor.ChannelInterceptorAdapter;

import cj.primariaiquest.dao.TicketEntityManager;
import cj.primariaiquest.dao.exceptions.DaoException;
import cj.primariaiquest.model.Ticket;

/**
 * Message interceptor used to intercept processed tickets and store them in
 * database.
 * 
 * @author parvu georgian
 * 
 */
public class StorageInterceptor extends ChannelInterceptorAdapter {
    private static Logger logger = Logger.getLogger(StorageInterceptor.class);

    @Override
    public void postSend(Message<?> message, MessageChannel channel,
	    boolean sent) {
	Ticket ticket = (Ticket) message.getPayload();
	TicketEntityManager tem = new TicketEntityManager();
	
	try {
	    tem.persist(ticket);
	} catch (DaoException e) {
	    System.out.println(e);
	    logger.error("Could not store " + ticket + "because"
		    + e.getStackTrace());
	}
    }

}
